package logic

import (
	"context"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"

	"week3/rpc/goods"
	"week3/rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

type Good struct {
	Id    int64
	Name  string
	Price float64
	Stock int64
}

func NewGoodsListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsListLogic {
	return &GoodsListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsListLogic) GoodsList(in *goods.Request) (*goods.GoodsListResponse, error) {
	db, err := gorm.Open(mysql.Open("root:q025l6263@tcp(43.143.123.142:3306)/learn"), &gorm.Config{})
	if err != nil {
		return &goods.GoodsListResponse{}, err
	}

	var gs []*goods.Good
	err = db.Find(&gs).Order("price").Error
	if err != nil {
		return &goods.GoodsListResponse{}, err
	}

	return &goods.GoodsListResponse{
		Gs: gs,
	}, err

}
